package com.jxb.myshop.services.impl;

import com.jxb.myshop.common.PageHelper;
import com.jxb.myshop.common.ResponseResult;
import com.jxb.myshop.dao.ProductDao;
import com.jxb.myshop.dao.impl.ProductDaoImpl;
import com.jxb.myshop.entity.Product;
import com.jxb.myshop.services.ProductServices;

import java.sql.SQLException;
import java.util.List;


/**
 * @author: COS
 * @time: 2022/9/17 9:19
 * @description:
 */
public class ProductServicesImpl implements ProductServices {
    private ProductDao pd = new ProductDaoImpl();

    @Override
    public ResponseResult getProById(int pid) {
        try {
            if (pid>0) {
               Product pro =  pd.getProById(pid);
               return ResponseResult.createSuccess(pro);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.createFail("没有这个商品");
    }

    @Override
    public ResponseResult getProByPageAndTid(int page, int tid) {
        try {
            //1 访问dao 获取数据总条数 需要按商品类型来获取
            Long count = pd.getTotalCount(tid);//select count(pid) from product;
            //2 创建分页工具
            PageHelper<Product> ph = new PageHelper<>(page, count.intValue(), 8);
            //3 访问dao 获取分页数据 也是需要商品类型数据
            List<Product> list = pd.getProByPageAndTid(tid,ph.getStartRow(),ph.getPageRow());
            //4 把数据封装到ph
            ph.setList(list);
            //5 封装ph到responseresult
            return ResponseResult.createSuccess(ph);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResponseResult.createFail("没有这个数据");
    }

    @Override
    public ResponseResult getAllProByTid(int tid) {
        try {
            List<Product> list = pd.getAllProByTid(tid);
            if (list != null && list.size() > 0) {
                return ResponseResult.createSuccess(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.createFail("没有这个类型商品数据");
    }

    @Override
    public ResponseResult getAll() {
        try {
            List<Product> all = pd.getAll();
            if (all != null && all.size() > 0) {
                return ResponseResult.createSuccess(all);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResponseResult.createFail("暂无此商品");
    }
}
